package com.learn.iot.ProcessorImpl;

import com.learn.iot.service.TestService;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.GenericBeanDefinition;

/**
 * 后置处理器，我这里的作用是用TestService去替换CityService
 *
 * 必须要要加Component注解，不然spring扫描不出来
 */
//@Component
public class MyBeanFactoryPostProcessorImpl implements BeanFactoryPostProcessor {

	@Override
	public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
		/**
		 * 这里我们用GenericBeanDefinition类来接收，GenericBeanDefinition是BeanDefinition的子类，GenericBeanDefinition有更多的api可用
		 */
		GenericBeanDefinition beanDefinition = (GenericBeanDefinition) beanFactory.getBeanDefinition("cityService");
		System.out.println(beanDefinition.getBeanClassName());
		beanDefinition.setBeanClass(TestService.class);
	}

}
